#!/bin/bash

function run_ffx {
	while true;
	do  
	    index=0
	    str=""
	    for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done
	    for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done
	    for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done
	    for i in {1..64}; do str="$str${arr[$RANDOM%$index]}"; done
	    keyword=${str:1:5}
	    any_cmt="any.comment"
	    ./firefox -no-remote -url www.google.fr/search?q=$keyword -comment $any_cmt
	    sleep 1;
	    ./firefox -no-remote -url www.bing.com/search?q=$keyword -comment $any_cmt
	    sleep 1;
	    
	    while read line; 
	    do 
	        echo $line
		./firefox -no-remote -url $line -comment $any_cmt 
		sleep 1;
	    done < "web";
	done
}


function check_ffx {
   sleep 10;
	while true;
	do
     # ffx_pid=`pgrep firefox`;
     ffx_pid=`ps aux | grep firefox | grep no-remote | awk '{print $2}' `; 
     run_time=`ps -eo pid,etime | egrep $ffx_pid | awk '{print $2}' | awk -F\: '{print $1}' `   # print run time in minutes...
     echo "run_time in minutes: "$run_time
     sleep 10;
     if [ $run_time -ge 4 ]; # if $run_time>=4min. kill ffx
     then
        echo "ffx is stuck.... kill it..">>log;date +"%Y%m%d%H%M">>log
        kill -9 $ffx_pid;
     fi;
   done;
}


function exit_handler {
   killall -9 exp.sh
}
trap exit_handler 2 # traps Ctrl-C (signal 2)



check_ffx & run_ffx 
























